import React, { useState } from 'react';
import { useChatContext } from '../contexts/ChatContext';

// 模型名称映射表 - 内部ID到显示名称
const MODEL_DISPLAY_NAMES = {
  'qwen-max': 'Qwen Max',
  'qwen-turbo': 'Qwen Turbo',
  'qwen-plus': 'Qwen Plus',
  'llama3-8b-instruct': 'Llama3 8B'
};

const ModelSelector = () => {
  const { state, dispatch } = useChatContext();
  const [isOpen, setIsOpen] = useState(false);

  const handleModelChange = (model) => {
    dispatch({ type: 'SET_MODEL', payload: model });
    setIsOpen(false);
  };

  // 获取当前选择模型的显示名称
  const getDisplayName = (modelId) => {
    return MODEL_DISPLAY_NAMES[modelId] || modelId;
  };

  return (
    <div className="relative">
      <button
        onClick={() => setIsOpen(!isOpen)}
        className="flex items-center space-x-2 px-4 py-2 rounded-md hover:bg-gray-100"
      >
        <span>{getDisplayName(state.selectedModel)}</span>
        <svg
          className={`w-4 h-4 transition-transform ${isOpen ? 'transform rotate-180' : ''}`}
          fill="none"
          stroke="currentColor"
          viewBox="0 0 24 24"
        >
          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
        </svg>
      </button>

      {isOpen && (
        <div className="absolute top-full left-0 mt-1 w-64 bg-white shadow-lg rounded-md py-1 z-10">
          {state.availableModels.map((model) => (
            <button
              key={model}
              className={`w-full text-left px-4 py-2 hover:bg-gray-100 ${model === state.selectedModel ? 'bg-blue-50 text-blue-600' : ''
                }`}
              onClick={() => handleModelChange(model)}
            >
              {getDisplayName(model)}
            </button>
          ))}
        </div>
      )}
    </div>
  );
};

export default ModelSelector;